package com.ccf.business.etl.init;

import com.ccf.business.etl.service.IComponentService;
import com.component.api.model.param.ComponentDefine;
import com.component.api.model.param.ComponentProperties;
import com.component.api.utils.ComponentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 描述：
 * 组件启动刷新组件属性
 * @author xianggj
 * @Date 2021/11/25 15:57
 **/
@Component
public class ComponentApplicationRunner implements ApplicationRunner {

    @Autowired
    private IComponentService iComponentService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        iComponentService.clear();
        List<ComponentProperties> list = ComponentUtils.loadComponentProperties();
        final Map<String, ComponentDefine> componentDefineMap = ComponentUtils.loadComponentDefine(list);
        List<com.ccf.business.etl.model.Component> components =
                list.stream().map(e -> new com.ccf.business.etl.model.Component(e, componentDefineMap.get(e.getCode()))).collect(Collectors.toList());
        iComponentService.saveBatch(components);
    }
}
